package com.mall.common.service.impl;

import java.awt.image.BufferedImage;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.mall.common.service.MyCaptchaService;
import com.octo.captcha.service.CaptchaService;

@Repository
public class CaptchaServiceImpl implements MyCaptchaService {

	@Resource private CaptchaService imageCaptchaService;
	//创建验证码图片
	public BufferedImage buildImage(String captchaId) {
		return (BufferedImage)imageCaptchaService.getChallengeForID(captchaId);
	}
	//验证验证码
	public boolean isValid(String captchaId, String captcha) {
		if (!StringUtils.isNotEmpty(captchaId) || !StringUtils.isNotEmpty(captcha)){
			return false;
		}
		try{
			return imageCaptchaService.validateResponseForID(captchaId, captcha.toUpperCase()).booleanValue();
		}catch(Exception e){
			return false;
		}
	}
}
